package com.doudizu.shoesonline.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doudizu.shoesonline.common.UserContext;
import com.doudizu.shoesonline.constant.UserConstant;
import com.doudizu.shoesonline.mapper.BrandMapper;
import com.doudizu.shoesonline.model.dto.brand.BrandDTO;
import com.doudizu.shoesonline.model.dto.brand.BrandQuery;
import com.doudizu.shoesonline.model.entity.Brand;
import com.doudizu.shoesonline.model.entity.Link;
import com.doudizu.shoesonline.model.entity.User;
import com.doudizu.shoesonline.model.vo.BrandVo;
import com.doudizu.shoesonline.service.BrandService;
import com.doudizu.shoesonline.service.LinkService;
import com.doudizu.shoesonline.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
* 
* @description 针对表【brand(品牌)】的数据库操作Service实现
* @createDate 2023-07-03 09:55:21
*/
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand>
    implements BrandService {

    @Resource
    private LinkService linkService;
    @Resource
    private UserService userService;

    @Override
    public Page<BrandVo> getBrands(BrandQuery brandQuery) {
        Integer pageNum = brandQuery.getPageNum();
        Integer pageSize = brandQuery.getPageSize();

        //TODO 可选分页
        if (pageNum==null || pageSize==null){
            pageNum=1;
            pageSize=5;
        }

        Page<Brand> objectPage = new Page<>(pageNum, pageSize);
        QueryWrapper<Brand> brandQueryWrapper = new QueryWrapper<>();
        User user = userService.getById(UserContext.getUserId());
        if (user != null && UserConstant.ADMIN_ROLE.equals(user.getRole())) {
            if(brandQuery != null && brandQuery.getStatus() != null){
                brandQueryWrapper.eq("status", brandQuery.getStatus());
            }
        }else{
            brandQueryWrapper.eq("status", 1);
        }
        brandQueryWrapper.like(StringUtils.isNotBlank(brandQuery.getBrandName()),"brand_name",brandQuery.getBrandName());
        Page<Brand> page = this.page(objectPage, brandQueryWrapper);
        List<Brand> records = page.getRecords();
        List<BrandVo> collect = getBrandVos(records);
        Page<BrandVo> brandVoPage = new Page<>();
        BeanUtil.copyProperties(page,brandVoPage);
        brandVoPage.setRecords(collect);
        return brandVoPage;
    }

    @Override
    public List<BrandVo> searchBrands(String brandName) {
        if (StringUtils.isBlank(brandName)){
            return this.getBrands(null).getRecords();
        }
        User loginUser = userService.getLoginUser();
        LambdaQueryWrapper<Brand> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String role = loginUser.getRole();
        if (UserConstant.ADMIN_ROLE.equals(role)){
            lambdaQueryWrapper.like(Brand::getBrandName, brandName);
            List<Brand> brandList = this.list(lambdaQueryWrapper);
            List<BrandVo> collect = getBrandVos(brandList);
            return collect;
        }
        LambdaQueryWrapper<Brand> brands = lambdaQueryWrapper.like(Brand::getBrandName, brandName).eq(Brand::getStatus, 1);
        List<Brand> brandList = this.list(brands);
        List<BrandVo> collect = getBrandVos(brandList);
        return collect;
    }

    @Override
    public BrandVo getBrand(String brandId) {
        Brand brand = this.getById(brandId);
        Integer pictureLinkId = brand.getPictureLinkId();
        Link link = linkService.getById(pictureLinkId);
        String linkUrl = link.getUrl();
        BrandVo brandVo = new BrandVo();
        brandVo.setUrl(linkUrl);
        BeanUtil.copyProperties(brand, brandVo);
        return brandVo;

    }

    @Override
    public void addBrand(String brandName,List<Integer> picIds) {
        Link link = new Link();
        link.setLinkName(brandName+"的logo");
        link.setId(picIds.get(0));
        linkService.updateById(link);
        Brand brand = new Brand();
        brand.setPictureLinkId(picIds.get(0));
        brand.setBrandName(brandName);
        this.save(brand);
        BrandVo brandVo = new BrandVo();
        brandVo.setUrl(link.getUrl());
        BeanUtil.copyProperties(brand,brandVo);
    }

    @Override
    public void updateBrand(BrandDTO brandDTO) {
        String brandName = brandDTO.getBrandName();
        Integer id = brandDTO.getId();
        List<Integer> picIds = brandDTO.getPicIds();

        Brand brand = new Brand();
        brand.setId(id);
        brand.setBrandName(brandName);
        if(picIds != null && picIds.size() != 0){
            // 修改link名
            Link link = new Link();
            link.setLinkName(brandName+"的logo");
            link.setId(picIds.get(0));
            linkService.updateById(link);
            brand.setPictureLinkId(picIds.get(0));
        }
        this.updateById(brand);
    }

    private List<BrandVo> getBrandVos(List<Brand> records) {
        List<BrandVo> collect = records.stream().map(brand -> {
            Integer pictureLinkId = brand.getPictureLinkId();
            Link link = linkService.getById(pictureLinkId);
            String linkUrl = link.getUrl();
            BrandVo brandVo = new BrandVo();
            brandVo.setUrl(linkUrl);
            BeanUtil.copyProperties(brand, brandVo);
            return brandVo;
        }).collect(Collectors.toList());
        return collect;
    }
}




